package com.oa.admin.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.joneying.common.annotation.CommonController;
import com.joneying.common.web.response.Resp;
import com.oa.admin.entity.UserHoliday;
import com.oa.admin.service.IHolidayService;
import com.oa.admin.service.IUserHolidayService;
import com.oa.admin.vo.UserHolidayListVO;
import com.oa.core.BaseController;
import com.oa.core.utils.DateUtils;
import com.oa.pm.entity.EntryInformation;
import com.oa.pm.service.IEntryInformationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Description;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Api(description = "用户假期")
@RequestMapping("/userHoliday")
@RestController
public class UserHolidayController extends BaseController {

    @Autowired
    private IUserHolidayService userHolidayService;

    @Autowired
    private IEntryInformationService entryInformationService;

    @Autowired
    private IHolidayService holidayService;

    @GetMapping("/list")
    @CommonController
    @Description("列表")
    @ApiOperation(value = "列表")
    public Resp<List<UserHolidayListVO>> findUserHolidayList(@RequestParam(required = false) Integer holidayType){
        EntryInformation entryInformation = entryInformationService.selectOne(new EntityWrapper<EntryInformation>().eq("user_id", getLoginUserId()));
        if(entryInformation == null){
            return new Resp().failure("入职信息未找到,请补全");
        }
        return new Resp().success(userHolidayService.findUserHolidayList(getLoginUserId(), holidayType));
    }

    @CommonController
    @Description("列表")
    @ApiOperation(value = "列表")
    @GetMapping("/user/list")
    public Resp<UserHolidayListVO> findUserHolidayListByUserId(@RequestParam(required = false)  String userId, @RequestParam(required = false) Integer holidayType) throws Exception {
        EntryInformation entryInformation = entryInformationService.selectOne(new EntityWrapper<EntryInformation>().eq("user_id", userId));
        if(entryInformation == null){
            return new Resp().failure("入职信息未找到,请补全");
        }
        List<UserHolidayListVO> userHolidayListVOS = userHolidayService.findUserHolidayList(userId, holidayType);

        return new Resp().success(userHolidayListVOS);
    }

    @PutMapping("")
    @CommonController
    @Description("修改")
    @ApiOperation(value = "修改")
    public Resp put(@RequestBody UserHoliday userHoliday){
        if(getLoginUserId() != null && getLoginUserId() != ""){
            if(userHolidayService.updateById(userHoliday)){
                return new Resp().success("修改成功");
            }
        }
        return new Resp().failure("修改失败，请重试");
    }

    @GetMapping("/export")
    @CommonController
    @Description("导出")
    @ApiOperation(value = "导出")
    public void export(@RequestParam(required = false) Integer holidayType){
        userHolidayService.export(getLoginUserId(), holidayType, response);
    }
}
